import webbrowser
import requests
import re

url = 'https://www.jd.com/?cu=true&utm_source=www.baidu.com&utm_medium=tuiguang&utm_campaign=t_1003608409_&utm_term=cef958b26f87497d89204a6fb88d1103'
try:
    htmlfile = requests.get(url)
    print(type(htmlfile))
except Exception as err:
    print("网页下载失败： %s" % err)

print('网页内容大小 = ', len(htmlfile.text), htmlfile.text)

# fn = 'file01.txt'
# fn = 'file01.html'
fn = 'file01.doc'
with open(fn, 'wb') as file_Obj:                     # 以二进制存储
    for diskStorage in htmlfile.iter_content(10240): # 循环取出内容
        size = file_Obj.write(diskStorage)           # 写入
        print(size)
        print("以 %s 存储网页html文件成功" % fn)


if htmlfile.status_code == requests.codes.ok:
    print('获取网页内容成功')
    pattern = input('请输入想要搜寻的内容')
    if pattern in htmlfile.text:
        print('搜寻 %s 成功' % pattern)
    else:
        print('搜寻 %s 失败' % pattern)
    name = re.findall(pattern, htmlfile.text)
    if name != None:
        print("%s 共出现 %d 次" % (pattern, len(name)))
    else:
        print("%s 出现0次" % pattern)
else:
    print('获取网页内容失败')

